/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_Modulo;
import conexion.Conexion;
import java.sql.Types;

/**
 * @author JoseHernan Created on : 28-sep-2014, 22:44:00
 */
public class DB_Modulo {

    public static ArrayList<B_Modulo> getList(int id) {
        ArrayList<B_Modulo> list = new ArrayList<B_Modulo>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            if (id == 0) {
                ps = con.prepareStatement(" SELECT id, nombre, descripcion FROM tbl_modulo where removed = false ");
            } else {
                ps = con.prepareStatement(" SELECT id, nombre, descripcion FROM tbl_modulo where removed = false and id = " + id);
            }
            rs = ps.executeQuery();
            B_Modulo obj;
            while (rs.next()) {
                obj = new B_Modulo();
                //Asign values
                obj.setId(rs.getInt(1));
                obj.setNombre(rs.getString(2));
                obj.setDescripcion(rs.getString(3));
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_Modulo obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_modulo(?,?,?,?,?,?)}");

            cs.registerOutParameter(1, Types.VARCHAR);
            cs.setString(2, action);
            cs.setInt(3, obj.getId());
            cs.setString(4, obj.getNombre());
            cs.setString(5, obj.getDescripcion());
            cs.setString(6, obj.getUser());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }

    public static ArrayList<B_Modulo> getPermisosModulos(int idUsuario) {
        ArrayList<B_Modulo> list = new ArrayList<B_Modulo>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            ps = con.prepareStatement(" SELECT pa.id_modulo, mo.nombre as modulo FROM tbl_rol_permisos rp inner join tbl_accion_pagina ap on ap.id = rp.id_accion_pagina inner join tbl_pagina pa on pa.id = ap.id_pagina inner join tbl_modulo mo on pa.id_modulo = mo.id inner join tbl_perfil pe on pe.id_rol = rp.id_rol where rp.removed = false and pe.id_usuario = " + idUsuario + " group by pa.id_modulo order by mo.nombre ");
            rs = ps.executeQuery();
            B_Modulo obj;
            while (rs.next()) {
                obj = new B_Modulo();
                //Asign values
                obj.setId(rs.getInt("id_modulo"));
                obj.setNombre(rs.getString("modulo"));
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }
}